# 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目。 
# 
#  给出两个整数 x 和 y，计算它们之间的汉明距离。 
# 
#  注意： 
# 0 ≤ x, y < 231. 
# 
#  示例: 
# 
#  
# 输入: x = 1, y = 4
# 
# 输出: 2
# 
# 解释:
# 1   (0 0 0 1)
# 4   (0 1 0 0)
#        ↑   ↑
# 
# 上面的箭头指出了对应二进制位不同的位置。
#  
#  Related Topics 位运算 
#  👍 400 👎 0


from typing import List


# leetcode submit region begin(Prohibit modification and deletion)
class Solution:
    def hammingDistance(self, x: int, y: int) -> int:
        xor = x ^ y
        distance = 0
        while xor != 0:
            if xor & 1 == 1:
                distance += 1
            xor = xor >> 1
        return distance





# leetcode submit region end(Prohibit modification and deletion)


def log(*args, **kwargs):
    print(*args, **kwargs)


# 异或后计算有多少位1
# 1. 内置函数: bin(x ^ y).count('1')
# 2. 不断向右位移, 判断末尾是否为1
#     def hammingDistance(self, x: int, y: int) -> int:
#         xor = x ^ y
#         distance = 0
#         while xor != 0:
#             if xor & 1 == 1:
#                 distance += 1
#             xor = xor >> 1
#         return distance
# 3. num 和 num - 1 做and运算,
# 1010 = 10
# 1001 = 9
# 1000
#
#     def hammingDistance(self, x: int, y: int) -> int:
#         xor = x ^ y
#         distance = 0
#         while xor != 0:
#             distance += 1
#             xor = xor & (xor - 1)
#         return distance


if __name__ == '__main__':
    s = Solution()
    x, y = 1, 4
    assert s.hammingDistance(x, y) == 2
